package com.zoe.onelink.business.interceptor;

import com.zoe.onelink.business.constant.RequestConst;
import com.zoe.onelink.util.OnelinkContextHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

/**
 * <p>标题: API接口拦截器</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2016</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: cxy
 * @date 2016/8/17
 */
public class BizHeaderInterceptor implements HandlerInterceptor {

    private static final Set<String> CONTEXT_HEADER_NAMES = Set.of(
            //当前语言类型
            RequestConst.HEADER_LANG_TYPE,
            //请求ID
            RequestConst.HEADER_REQUEST_ID,
            //操作人
            RequestConst.HEADER_OPERATOR,
            //所属科室
            RequestConst.HEADER_DEPT_CODE,
            //所属院区
            RequestConst.HEADER_DISTRICT_CODE,
            //所属机构
            RequestConst.HEADER_ORG_CODE,
            //客户端ID
            RequestConst.HEADER_CLIENT_ID,
            //客户端IP
            RequestConst.HEADER_CLIENT_IP,
            //客户端名称
            RequestConst.HEADER_CLIENT_NAME
    );

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        CONTEXT_HEADER_NAMES.forEach(headerName -> OnelinkContextHolder.putIfNotBlank(headerName, request.getHeader(headerName)));
        return true;
    }

}
